import { defineStore } from 'pinia';
import { UserControllerService, IRequestOptions } from '../openapi/api';
import { LoginUserVO } from '../openapi/api.ts';

export const useUserStore = defineStore('user', {
  state: () => ({
    isLoggedIn: false,  // 用户是否登录
    user: {} as LoginUserVO, // 用户信息
    isLoginModalVisible : false as boolean, // 登录弹窗的可见性,
    isInitialized: false
  }),
  actions: {
    async checkLoginStatus() {
      if (this.isLoggedIn) {
        return; 
      }
      const response = await UserControllerService.getLoginUserUsingGet();
      if (response.data && response.data.id) {
        this.isLoggedIn = true;
        this.user = response.data;
        this.isInitialized = true;
      } else {
        this.isLoggedIn = false;
        this.user = null;
        this.isInitialized = false;
      }
    },
    showLoginModal(){
      this.isLoginModalVisible = true;
    },
    hideLoginModal(){
      this.isLoginModalVisible = false
    }
  }
});
